<template>
  <el-dialog id="edit-dialog" :title="formValidate.id ? $t('appMarketing.popUps.edit') : $t('appMarketing.popUps.add')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('appMarketing.popUps.table.name')" prop="name">
        <el-input v-model="formValidate.name" clearable :placeholder="$t('appMarketing.popUps.prompt.name')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('appMarketing.popUps.table.shelfPeriod')" prop="time">
        <el-date-picker
          v-model="time"
          type="daterange"
          range-separator="~"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"
        />
      </el-form-item>
      <el-form-item :label="$t('appMarketing.popUps.table.img')" prop="file">
        <up-load-image
          ref="upload"
          :img-width="150"
          :img-height="150"
          :img-url="formValidate.url"
          @getUrl="getImgUrl"
        />
        {{ $t('appMarketing.recommend.recommendedSize') }} 600px * 800px
      </el-form-item>
      <el-form-item :label="$t('appMarketing.popUps.table.jump')" prop="jump">
        <el-select v-model="formValidate.jump" clearable :placeholder="$t('appMarketing.popUps.prompt.jump')" style="width: 100%">
          <el-option
            v-for="item in $t('appMarketing.popUps.jump')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
        <el-input v-if="formValidate.jump" v-model="formValidate.jumpLink" clearable :placeholder="$t('appMarketing.popUps.prompt.jumpLink')" :maxlength="500" style="margin-top: 10px" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPopUP, updatePopUP } from '@/api/operation'
import UpLoadImage from '@/components/product/up-load-image.vue'
import { formatDate } from '@/utils/method'
export default {
  name: 'PopUpsDialog',
  components: {
    UpLoadImage
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateTime = (rule, value, callback) => {
      if (this.time && this.time.length > 1) {
        callback()
      } else {
        callback(new Error(this.$t('appMarketing.popUps.table.shelfPeriod') + this.$t('common.null')))
      }
    }
    const validateFile = (rule, value, callback) => {
      if (this.formValidate.id) {
        if (this.formValidate.url || this.file) {
          callback()
        } else {
          callback(new Error(this.$t('appMarketing.popUps.table.img') + this.$t('common.null')))
        }
      } else {
        if (this.file) {
          callback()
        } else {
          callback(new Error(this.$t('appMarketing.popUps.table.img') + this.$t('common.null')))
        }
      }
    }
    const validateJump = (rule, value, callback) => {
      if (this.formValidate.jump != null) {
        if (this.formValidate.jump) {
          if (this.formValidate.jumpLink) {
            callback()
          } else {
            callback(new Error(this.$t('appMarketing.popUps.prompt.jumpLink')))
          }
        } else {
          callback()
        }
      } else {
        callback(new Error(this.$t('appMarketing.popUps.table.jump') + this.$t('common.null')))
      }
    }
    return {
      submitLoading: false,
      time: null,
      file: null,
      formValidate: {
        id: null,
        name: null,
        jump: null,
        putawayBeginTime: null,
        putawayEndTime: null,
        jumpLink: null,
        url: null
      },
      rules: {
        name: [{ required: true, message: this.$t('appMarketing.popUps.table.name') + this.$t('common.null'), trigger: 'blur' }],
        time: [{ required: true, validator: validateTime, trigger: 'blur' }],
        file: [{ required: true, validator: validateFile, trigger: 'blur' }],
        jump: [{ required: true, validator: validateJump, trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    getImgUrl(file) {
      console.log(file)
      this.file = file
    },
    setTime(start, end) {
      this.time = [start, end]
    },
    handleClose() {
      this.$emit('change-dialog', 'editDialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = new FormData()
          data.append('id', this.formValidate.id)
          data.append('name', this.formValidate.name)
          data.append('jump', this.formValidate.sort)
          data.append('putawayBeginTime', formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00')
          data.append('putawayEndTime', formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59')
          if (this.formValidate.jump === true) {
            data.append('jumpLink', this.formValidate.jumpLink)
          }
          if (this.file) {
            data.append('file', this.file)
          } else {
            if (this.formValidate.url) {
              data.append('url', this.formValidate.url)
            }
          }
          if (this.formValidate.id) {
            updatePopUP(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addPopUP(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>

